<?php

namespace app\common\model;

use think\Model;
use think\Cache;

class Label extends Model
{


    /**
     * 行业
     * @param number $industry_id
     * @return mixed
     */
    public function industry($industry_id = 0)
    {

        $cache = new Cache();
        $result = $cache->get('industry_' . $industry_id);
        if ($result) {
            $result = unserialize($result);
        } else {
            $data = db('industry')->field('id,industry_name,pid')->select();
            $result = $this->arr2tree($data, $industry_id);
            $cache->set('industry_' . $industry_id, serialize($result), 3600);
        }


        return $result;
    }

    /**
     * 城市
     * @param number $city_id
     * @return mixed
     */
    public function city($city_id = 0)
    {

        $cache = new Cache();
        $result = $cache->get('city_' . $city_id);
        if ($result) {
            $result = unserialize($result);
        } else {
            $data = db('city')->field('id,city_name,pid')->select();
            $result = $this->arr2tree($data, $city_id);
            $cache->set('city_' . $city_id, serialize($result), 3600);
        }


        return $result;
    }

    /**
     * 产业链
     * @param number $chain_id
     * @return mixed
     */
    public function chain($chain_id = 0,$user_id=1)
    {
        //加载用户权限
        $users=db('users')->find($user_id);
        if(empty($users)||empty($users['chain_id'])){
            return [];
        }

        $cache = new Cache();
        $result = $cache->get('chain_' . $chain_id);
        //Cache::clear();die();
        if ($result && 1 == 2) {
            $result = unserialize($result);
        } else {
            $data = db('chain')->field('id,chain_name,pid')->where("(pid=0 and id in ({$users['chain_id']}) and status=1 ) or  (status=1 and pid != 0)")->order('sort_no', 'desc')->select();
            $result = $this->arr2tree($data, $chain_id);
            $cache->set('chain_' . $chain_id, serialize($result), 3600);
        }


        return $result;
    }

    // 将数据按照所属关系封装 见图2
    private function arr2tree($tree, $rootId = 0)
    {
        $return = array();
        foreach ($tree as $leaf) {
            if ($leaf['pid'] == $rootId) {
                foreach ($tree as $subleaf) {
                    if ($subleaf['pid'] == $leaf['id']) {
                        $leaf['children'] = $this->arr2tree($tree, $leaf['id']);
                        break;
                    }
                }
                $return[] = $leaf;
            }
        }
        return $return;
    }


    /**
     * 递归出父集
     * @param $cate
     * @param $id
     * @return array
     */
    function get_top_parentid($cate,$id){
        $arr=array();
        foreach($cate as $v){
            if($v['id']==$id){
                $arr[]=$v;// $arr[$v['id']]=$v['name'];
                $arr=array_merge($this->get_top_parentid($cate,$v['p_id']),$arr);
            }
        }return $arr;

    }
    /**
     * 产业链
     * @param number $chain_id
     * @return mixed
     */
    public function chain_show($chain_id = 0, $params = [])
    {
        $cache = new Cache();
        $level=db('chain')->where(['status'=>1,'id'=>$chain_id])->value('level');
        $cate=db('chain')->field('id,pid as p_id')->where(['status'=>1])->select();
        if ($level!=0){
            $chain_data=$this->get_top_parentid($cate,$chain_id);
            $chain_id=min(array_column($chain_data,'id'));
        }
        //Cache::clear();
        $result = $cache->get('chain_show_' . $chain_id .'_city_'.$params['city_id']. '_type_' . $params['type']);
        if ($result && 1 == 2) {
            $result = unserialize($result);
        } else {
            $data = db('chain')->field('id,chain_name,pid,children_chain')->order('sort_no','desc')->where(['chain' => $chain_id])->select();
            /*p($chain_id);*/
            $result = $this->arr2tree_show($data, $chain_id, $params);
            $cache->set('chain_show_' . $chain_id .'_city_'.$params['city_id']. '_type_' . $params['type'], serialize($result), 3600);
        }


        return $result;
    }

    // 将数据按照所属关系封装 见图2
    private function arr2tree_show($tree, $rootId = 0, $params = [])
    {
        switch ($params['type']) {
            case "company":
                $obj = db('companys a')->where('a.status', 1);
                $return = array();
                foreach ($tree as $key => $leaf) {
                    /***关键字*/
                    if (!empty($params['keywords'])) {
                        $keywords = $params['keywords'];
                        $obj->where('a.company_name', 'like', '%' . $keywords . '%');
                    }
                    /***行业*/
                    if (!empty($params['industry_id'])) {
                        $industry_id = $params['industry_id'];
                        $obj->join('companys_industry b', 'a.id=b.company_id', 'LEFT')->where('b.status', 1)->where('b.industry_id', $industry_id);
                    }
                    /***城市*/
                    if (!empty($params['city_id'])) {
                        $city_id = $params['city_id'];
                        $obj->where("a.province_id=$city_id or a.city_id=$city_id or a.area_id=$city_id");
                    }
                    /***园区*/
                    if (!empty($params['park_id'])) {
                        $park_id = $params['park_id'];
                        $obj->where('a.park_id', $park_id);
                    }
                    $info = $obj->join("companys_chain c", "a.id=c.company_id", 'LEFT')->field("a.id")->where('c.chain_id', $leaf['id'])->find();
                    if ($info) {
                        $tree[$key]['flag'] = 1;
                    } else {
                        $tree[$key]['flag'] = 0;
                    }
                }
                break;
            case "park":
                $obj = db('parks a')->where('a.status', 1);
                $return = array();

                foreach ($tree as $key => $leaf) {
                    /***关键字*/
                    if (!empty($params['keywords'])) {
                        $keywords = $params['keywords'];
                        $obj->where('a.park_name', 'like', '%' . $keywords . '%');
                    }
                    /***行业*/
                    if (!empty($params['industry_id'])) {
                        $industry_id = $params['industry_id'];
                        $obj->join('parks_industry b', 'a.id=b.park_id', 'LEFT')->where('b.status', 1)->where('b.industry_id', $industry_id);
                    }
                    /***城市*/
                    if (!empty($params['city_id'])) {
                        $city_id = $params['city_id'];
                        //$obj->where('a.province_id', $city_id)->whereOr('a.city_id', $city_id)->whereOr('a.area_id', $city_id);
                        $obj->where("a.province_id=$city_id or a.city_id=$city_id or a.area_id=$city_id");
                    }
                    /***园区*/
                    if (!empty($params['park_id'])) {
                        $park_id = $params['park_id'];
                        $obj->where('a.park_id', $park_id);
                    }
                    $info = $obj->join("parks_chain c", "a.id=c.park_id", 'LEFT')->field("a.id")->where('c.chain_id', $leaf['id'])->find();
                    if ($info) {
                        $tree[$key]['flag'] = 1;
                    } else {
                        $tree[$key]['flag'] = 0;
                    }
                }
                break;
            default:
                exit('未知类型');
        }

        foreach ($tree as $leaf) {


            if ($leaf['pid'] == $rootId) {
                foreach ($tree as $subleaf) {
                    if ($subleaf['pid'] == $leaf['id']) {
                        $leaf['children'] = $this->arr2tree_show($tree, $leaf['id'], $params);
                        break;
                    } else {
                        $leaf['children'] = [];
                    }
                }
                $return[] = $leaf;
            }
        }
        return $return;
    }

    /**
     * 城市
     * @param number $city_id
     * @return mixed
     */
    public function city_list()
    {

        $cache = new Cache();
        $result = $cache->get('city_list' );
        if ($result) {
            $result = unserialize($result);
        } else {
            $data_1 = db('city')->field('id,city_name,pid')->where(['type'=>1])->select();
            $data_2 = db('city')->field('id,city_name,pid')->where(['type'=>2])->select();
            $data_3 = db('city')->field('id,city_name,pid')->where(['type'=>3])->select();


            $result = ['province'=> $data_1,'city'=>$data_2,'area'=> $data_3 ];
            $cache->set('city_list', serialize($result), 3600);
        }


        return $result;
    }


}